package list.model;

import list.control.MysqlLink;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by ZhipengLi.
 */
public class Contact extends MysqlLink {
    private static final String TABLE = "contact";

    private int contact_id;

    private int user_id;

    private String name;

    private String nickname;

    private String email;

    private String birthday;

    private String head;

    private String work_place;

    private String postcode;

    private String address;

    private int tag_id;

    private String wechat;

    private String qq;

    private String alipay;

    private Telephone[] telephones;

    private Contact(int contact_id, int user_id, String name, String nickname, String email, String birthday, String head, String work_place, String postcode, String address, int tag_id, String wechat, String qq, String alipay) {
        this.contact_id = contact_id;
        this.user_id = user_id;
        this.name = name;
        this.nickname = nickname;
        this.email = email;
        this.birthday = birthday;
        this.head = head;
        this.work_place = work_place;
        this.postcode = postcode;
        this.address = address;
        this.tag_id = tag_id;
        this.wechat = wechat;
        this.qq = qq;
        this.alipay = alipay;
    }

    public static boolean insert(int user_id, int tag_id) {
        return insert(TABLE, toArray("user_id", "tag_id", "name"), toArray(user_id, true, "NEW FRIEND"));
    }

    public static boolean delete(int user_id, int tag_id, int contact_id) {
        return delete(TABLE, equalsAnd(toArray("user_id", "tag_id", "contact_id"), toArray(user_id, tag_id, contact_id))) && Tag.decrease(user_id, tag_id);
    }

    public static boolean delete(int user_id, int tag_id) {
        return delete(TABLE, equalsAnd(toArray("user_id", "tag_id"), toArray(user_id, tag_id)));
    }

    public static boolean update(int user_id, int contact_id, String property, Object value) {
        String[] fields = {"name", "nickname", "email", "birthday", "head", "work_place", "postcode", "address", "tag_id", "wechat", "qq", "alipay"};
        for (String field : fields) {
            if (field.equals(property)) {
                return update(TABLE, property, value, equalsAnd(toArray("user_id", "contact_id"), toArray(user_id, contact_id)));
            }
        }
        return false;
    }

    public static Contact[] select(int user_id, int tag_id) {
        ResultSet resultSet = select(TABLE, ALL, equalsAnd(toArray("user_id", "tag_id"), toArray(user_id, tag_id)));
        return resultOfSelect(resultSet);
    }

    public static Contact[] select(int user_id) {
        ResultSet resultSet = select(TABLE, ALL, equal("user_id", user_id));
        return resultOfSelect(resultSet);
    }

    private static Contact[] resultOfSelect(ResultSet resultSet) {
        if (null != resultSet) {
            try {
                ArrayList<Contact> contacts = new ArrayList<>();
                while (resultSet.next()) {
                    int contact_id = resultSet.getInt("contact_id");
                    int user_id = resultSet.getInt("user_id");
                    int tag_id = resultSet.getInt("tag_id");
                    String name = resultSet.getString("name");
                    String nickname = resultSet.getString("nickname");
                    String email = resultSet.getString("email");
                    String birthday = resultSet.getString("birthday");
                    String head = resultSet.getString("head");
                    String work_place = resultSet.getString("work_place");
                    String postcode = resultSet.getString("postcode");
                    String address = resultSet.getString("address");
                    String wechat = resultSet.getString("wechat");
                    String qq = resultSet.getString("qq");
                    String alipay = resultSet.getString("alipay");
                    contacts.add(new Contact(contact_id, user_id, name, nickname, email, birthday, head, work_place, postcode, address, tag_id, wechat, qq, alipay));
                }
                resultSet.close();
                if (0 < contacts.size()) {
                    Contact[] contactList = new Contact[contacts.size()];
                    contacts.toArray(contactList);
                    return contactList;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /*public static User search(int user_id, String nameOrNickname) {

    }

    public static User search(int user_id, int phone_number) {

    }*/

    public Telephone[] getTelephones() {
        if (null == this.telephones) {
            this.telephones = Telephone.select(contact_id);
        }
        return this.telephones;
    }


    public int getContact_id() {
        return contact_id;
    }

    public int getUser_id() {
        return user_id;
    }

    public String getName() {
        return name;
    }

    public String getNickname() {
        return nickname;
    }

    public String getEmail() {
        return email;
    }

    public String getBirthday() {
        return birthday;
    }

    public String getHead() {
        return head;
    }

    public String getWork_place() {
        return work_place;
    }

    public String getPostcode() {
        return postcode;
    }

    public String getAddress() {
        return address;
    }

    public int getTag_id() {
        return tag_id;
    }

    public String getWechat() {
        return wechat;
    }

    public String getQq() {
        return qq;
    }

    public String getAlipay() {
        return alipay;
    }
}
